package com.dyh.quickdemo.beiya;

import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.BaseSubscriber;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/beiya")
@Slf4j
public class BeiYaController {
    List<String> datas = new ArrayList<>();
    Flux<String> source=Flux.fromIterable(datas);

    Subscriber subscriber = new BaseSubscriber<String>() {
        @Override
        protected void hookOnSubscribe(Subscription subscription) {
            request(1);
        }
        @Override
        protected void hookOnNext(String value) {
            log.info(value);
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            request(1);
        }
    };


    @RequestMapping("/addSource")
    public Object addSource(@RequestBody List<String> datas){
       // source = Flux.fromIterable(datas);
        this.datas = datas;
        return "ok";
    }

    @RequestMapping("/subscriber")
    public Object subscriber(){

//        source.map(String::toUpperCase)
        source.subscribe(subscriber);
        return "ok";
    }


}
